package com.heifeng.cloudshelf.mvp.programme.adapter;

import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.dao.ProgrammeBean;
import com.heifeng.cloudshelf.event.DialogCaneclEvent;
import com.heifeng.cloudshelf.event.DialogDismissEvent;
import com.heifeng.cloudshelf.event.DialogFinishEvent;
import com.heifeng.cloudshelf.event.MoreClickEvent;
import com.heifeng.cloudshelf.event.SpecTextEvent;
import com.heifeng.cloudshelf.mvp.model.ProgrammeSelectInfo;
import com.heifeng.cloudshelf.mvp.model.SpecMultipleEntity;
import com.heifeng.cloudshelf.mvp.programme.presenter.ProgrammeActivityPresenter;
import com.heifeng.cloudshelf.widget.BottomDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2019/9/19.
 */

public class SpecTextRvAdapter extends BaseQuickAdapter<ProgrammeSelectInfo.OptionBean, BaseViewHolder> {
    private List<ProgrammeSelectInfo.OptionBean> data;

    private List<ProgrammeSelectInfo> infos;

    private int selectPosition = 0;
    private int dataSize;
    private int position;
    private boolean isChangesItems = false;

    private BottomDialog bottomSheet;

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public SpecTextRvAdapter(Context context, int layoutResId, @Nullable List<ProgrammeSelectInfo.OptionBean> data, List<ProgrammeSelectInfo> infos, int dataSize,int position) {
        super(layoutResId, data);
        mContext = context;
        this.data = data;
        this.infos = infos;
        this.dataSize = dataSize;
        this.position = position;

//        initBottomDialog();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ProgrammeSelectInfo.OptionBean item) {
        if (item.isSelected()) {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_chosen_sel));
        } else {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_unchosen_sel));
        }

        if (helper.getPosition() == 8) {
//        if (helper.getPosition() == 8 && dataSize > 9) {
            helper.getView(R.id.iv_more).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_text, "更多");
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_unchosen_sel));
        } else {
            helper.getView(R.id.iv_more).setVisibility(View.GONE);
            helper.setText(R.id.tv_text, item.getSpec_name());
        }

        helper.getView(R.id.l_string).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (helper.getPosition() == 8) {

                    notifyDataSetChanged();

                    EventBus.getDefault().post(new MoreClickEvent());

//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            initBottomDialog();
//                            bottomSheet.show();
//                        }
//                    }, 500);    //延时

                    if (null == bottomSheet || !bottomSheet.isShowing()) {
                        initBottomDialog();
                        bottomSheet.show();
                    }

                    return;
                }
                if (!item.isSelected()) {
                    setSelect();
                    item.setSelected(true);
                    notifyDataSetChanged();

                    if (helper.getPosition() != 8) {
                        EventBus.getDefault().post(new SpecTextEvent(item.getGroupId(), item.getParentId(), item.getSpec_name(), item.getId()));
                    }
                }

            }
        });
    }

    private void setSelect() {
        List<ProgrammeSelectInfo.OptionBean> options = infos.get(position).getOptions();
        for (int i = 0; i < options.size(); i++) {
            options.get(i).setSelected(false);
        }
//        if (null != adapter) {
//            adapter.notifyDataSetChanged();
//        }
    }

    private LinearLayoutManager layoutManager;

    private SpecMultipleItemRvAdapter adapter;

    private List<SpecMultipleEntity> mData = new ArrayList<>();

    //初始化底部弹窗
    private void initBottomDialog() {
        if (mData.size() > 0) {
            mData.clear();
        }
        bottomSheet = new BottomDialog(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_bottom_dialog_rv, null, false);
        bottomSheet.setCancelable(true);
        bottomSheet.setContentView(view);
        bottomSheet.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(R.color.bg_1c2428);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvFinish = (TextView) view.findViewById(R.id.tv_finish);

        for (int i = 0; i < infos.size(); i++) {
            mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW_DIALOG, i));
        }

        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new SpecMultipleItemRvAdapter(mContext, mData, infos);
        recyclerView.setAdapter(adapter);

        if (0 != ProgrammeActivityPresenter.type) {
            DialogSpecTextRvAdapter.setOnSpecChangedListener(new DialogSpecTextRvAdapter.OnSpecChangedListener() {
                @Override
                public void onChanged(int specTextId, int parentId, int index) {
                    setSpecList(specTextId, parentId, index);
                    Log.e("SpecEvent","setSpecList specTextId:"+specTextId);
                }
            });
        }

        bottomSheet.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Log.e("TAGS","dialog show");
//                EventBus.getDefault().post(new DialogDismissEvent());
//                if (null != adapter) {
//                    adapter.notifyDataSetChanged();
//                }
                EventBus.getDefault().post(new DialogCaneclEvent(true,isChangesItems));
            }
        });

        bottomSheet.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                EventBus.getDefault().post(new DialogDismissEvent());
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new DialogCaneclEvent(false,isChangesItems));
                bottomSheet.dismiss();
            }
        });

        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new DialogFinishEvent());
                bottomSheet.dismiss();
            }
        });
    }

    public void setSpecList(int specTextId, int parentId, int position) {
        Log.e("SpecTextRvAdapter", "specTextId:" + specTextId + ",parentId:" + parentId);

        if (0 == parentId && null != ProgrammeActivityPresenter.allSpecList && ProgrammeActivityPresenter.allSpecList.size() > 0) {
            boolean isSelAll = true;
            boolean isRelation = false;

            List<Integer> selIds = new ArrayList<>();

            //遍历已选择的选项
            for (int i = 0; i < infos.size(); i++) {
                List<ProgrammeSelectInfo.OptionBean> options = infos.get(i).getOptions();
                for (int j = 0; j < options.size(); j++) {
                    ProgrammeSelectInfo.OptionBean optionBean = options.get(j);
                    Log.e("SpecTextRvAdapter", "optionBean:" + optionBean);
                    if (optionBean.isSelected() && optionBean.getId() != specTextId) {
                        selIds.add(optionBean.getId());
                        if (0 != optionBean.getId()) {
                            isSelAll = false;
                        }
                    }
                }
            }

            //选择全部按钮，重置所有选项
            if (isSelAll && 0 == specTextId) {
                reSetData();
                return;
            }

            //判断点击全部按钮后，该列是否是属于需要联动的按钮
            if (0 == specTextId && infos.size() > position - 1) {
                boolean isOtherRelation = false;
                int id = infos.get(position - 1).getOptions().get(1).getId();
                //遍历按钮是否存在关联
                for (int i = 0; i < ProgrammeActivityPresenter.allSpecList.size(); i++) {
                    for (int j = 0; j < ProgrammeActivityPresenter.allSpecList.get(i).getOptions().size(); j++) {
                        if (id == ProgrammeActivityPresenter.allSpecList.get(i).getOptions().get(j).getParentId()) {
                            isOtherRelation = true;
                            break;
                        }
                    }
                    if (isOtherRelation) {
                        break;
                    }
                    Log.e("setSpecList", "for isOtherRelation:" + isOtherRelation);
                }

                if (isOtherRelation) {
                    //重置选项
                    reSetData();
                    //回复已选的且并不关联的选项
                    for (int i = 0; i < infos.size(); i++) {
                        List<ProgrammeSelectInfo.OptionBean> options = infos.get(i).getOptions();
                        for (int j = 0; j < options.size(); j++) {
                            ProgrammeSelectInfo.OptionBean optionBean = options.get(j);
                            if (selIds.contains(optionBean.getId()) && 0 == optionBean.getParentId()) {
                                optionBean.setSelected(true);
                                options.get(0).setSelected(false);
                                break;
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                    return;
                }
            }


            //遍历按钮是否存在关联
            for (int i = 0; i < ProgrammeActivityPresenter.allSpecList.size(); i++) {
                for (int j = 0; j < ProgrammeActivityPresenter.allSpecList.get(i).getOptions().size(); j++) {
                    if (0 != specTextId && specTextId == ProgrammeActivityPresenter.allSpecList.get(i).getOptions().get(j).getParentId()) {
                        isRelation = true;
                        break;
                    }
                }
                if (isRelation) {
                    break;
                }
            }

            if (!isRelation) {
                return;
            }
            isChangesItems = true;

            Log.e("SpecTextRvAdapter", "size:" + selIds.size() + ",selIds:" + selIds);

            List<ProgrammeSelectInfo> newInfos = new ArrayList<>();

            for (int i = 0; i < ProgrammeActivityPresenter.allSpecList.size(); i++) {
                boolean isSelected = false;

                ProgrammeBean.SpecListBean info = ProgrammeActivityPresenter.allSpecList.get(i);

                String spec_name = info.getSpec_name();
                List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();

                List<ProgrammeBean.SpecListBean.OptionsBean> options = info.getOptions();
                if (2 == ProgrammeActivityPresenter.type && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                } else {
                    optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, i + 1, 0 - i, "全部"));
                }
                for (int j = 0; j < options.size(); j++) {
                    ProgrammeBean.SpecListBean.OptionsBean optionsBean = options.get(j);
                    if (!isSelected) {
                        isSelected = (optionsBean.getId() == specTextId);
                    }
                    if (0 == optionsBean.getParentId()) {
                        optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));

                    } else {

                        if (optionsBean.getParentId() == specTextId) {
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
                        }

                        if (selIds.contains(optionsBean.getParentId())) {
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
                        }

//                        if (!isRelation) {
//                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
//                        }
                    }
                }


                if (optionBeans.size() > 1) {
                    ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
                    selectInfo.setTitle(spec_name);

                    int index = 0;
                    if (isSelected) {
                        if ("全部".equals(optionBeans.get(0).getSpec_name())) {
                            optionBeans.get(0).setSelected(false);
                        }
                    } else {
                        for (int j = 0; j < optionBeans.size(); j++) {
                            ProgrammeSelectInfo.OptionBean optionBean = optionBeans.get(j);
                            if (selIds.contains(optionBean.getId())) {
                                index = j;
                            } else {
                                optionBean.setSelected(false);
                            }
                        }
                        optionBeans.get(index).setSelected(true);
                    }

                    selectInfo.setOptions(optionBeans);
                    if (spec_name.equals(mContext.getResources().getString(R.string.text_space))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_space);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_modelling)) || spec_name.equals(mContext.getResources().getString(R.string.text_category)) || spec_name.equals("品牌")) {
                        selectInfo.setIcon(R.mipmap.ic_classifiton_modelling);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_style)) || spec_name.equals("功能") || spec_name.equals(mContext.getResources().getString(R.string.text_serious))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_style);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                        selectInfo.setIcon(R.mipmap.ic_classifition_type);
                    }
                    newInfos.add(selectInfo);
                }
            }
            Log.e("SpecTextRvAdapter", "newInfos size:" + newInfos.size());

            if (newInfos.size() > 0) {
                Log.e("SpecTextRvAdapter", "newInfos:" + newInfos);
                infos.clear();
                infos.addAll(newInfos);
                mData.clear();
                for (int i = 0; i < infos.size(); i++) {
                    mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW_DIALOG, i));
                }
                adapter.setNewData(mData);
                adapter.notifyDataSetChanged();
                Log.e("SpecTextRvAdapter", "adapter update");
            }
        }
    }

    private void reSetData() {
        if (null == ProgrammeActivityPresenter.allSpecList || ProgrammeActivityPresenter.allSpecList.size() == 0) {
            return;
        }
        infos.clear();
        mData.clear();
        for (int i = 0; i < ProgrammeActivityPresenter.allSpecList.size(); i++) {
            String spec_name = ProgrammeActivityPresenter.allSpecList.get(i).getSpec_name();
            List<ProgrammeBean.SpecListBean.OptionsBean> options = ProgrammeActivityPresenter.allSpecList.get(i).getOptions();
            List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();
            if (2 == ProgrammeActivityPresenter.type && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
            } else {
                optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, i + 1, 0 - i, "全部", true));
            }
            for (int j = 0; j < options.size(); j++) {
                optionBeans.add(new ProgrammeSelectInfo.OptionBean(options.get(j).getParentId(), i + 1, options.get(j).getId(), options.get(j).getOptionName()));
            }
            ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
            selectInfo.setTitle(spec_name);
            selectInfo.setOptions(optionBeans);
            if (spec_name.equals(mContext.getResources().getString(R.string.text_space))) {
                selectInfo.setIcon(R.mipmap.ic_classification_space);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_modelling)) || spec_name.equals(mContext.getResources().getString(R.string.text_category)) || spec_name.equals("品牌")) {
                selectInfo.setIcon(R.mipmap.ic_classifiton_modelling);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_style)) || spec_name.equals("功能") || spec_name.equals(mContext.getResources().getString(R.string.text_serious))) {
                selectInfo.setIcon(R.mipmap.ic_classification_style);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                selectInfo.setIcon(R.mipmap.ic_classifition_type);
            }
            infos.add(selectInfo);
        }
        for (int i = 0; i < infos.size(); i++) {
            mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW_DIALOG, i));
        }
        adapter.setNewData(mData);
    }
}
